%% this code is to produce data for figure 6
cd ('financing_competitors_upload\code\');


clear 
%%
%%% Decompose into funding and cannibalization
rno = 4.52;
rnb = 4.45;
rb = 4.4;
rm = 4.25
outside = 1-0.2-0.24-0.18-0.05;

sigma = 0.3
F= 15754*354500 % mean number of potential buyers * average mortgage size 
alpha = 1.14;
qb = 2.43; 
qn = 1.67;
k = 3.21;
psim = 3.5;
psiw = 3.28;
eta = 0.09;
rhob = 3.69;
rhoo = 4.38;
annuity_factor = (1-(1+0.04)^(-3))/0.04

%% find mean, top decile and bottom decile 
player_5 = [11;7;6]; % nLS, nNLS, nMB, nFB
player_50 = [24;11;8]; % mean 
player_95 = [44;12;10];

% first -- cannibalization 
sigma = 0.3

% across markets 
[rno1,sno1,rnb1,snb1,rm1,sbm1,rb1,sb1, rhob1, sbw1, sow1,markup11,markup21] = Solve_local(F,sigma,psim,psiw,alpha,k,player_5(3),player_5(1),player_5(2),qn,qb,eta,rhoo);
% shut down cannibalization
[rno2,sno2,rnb2,snb2,rm2,sbm2,rb2,sb2, rhob2, sbw2, sow2,markup12,markup22] = Solve_local_nocan(F,sigma,psim,psiw,alpha,k,player_5(3),player_5(1),player_5(2),qn,qb,eta,rhoo);

d_rate = rhob1-rhob2 %0.0167%
d_local_financed = 100*(sbw1*player_5(1)*player_5(3)-sbw2*player_5(1)*player_5(3))
%-6.8bps
d_mort_rate = 100*(rnb1-rnb2) % 0.148bps
d_bank_rate = 100*(rb1-rb2) % 0.004bps
surplus1 =annuity_factor*( (log(player_5(3)*exp(-alpha*rb1+qb)+player_5(2)*exp(-alpha*rm1+qb)+player_5(1)*sow1*exp(-alpha*rno1+qn)+player_5(1)*sbw1*player_5(1)*exp(-alpha*rnb1+qn))/alpha))
surplus2 =annuity_factor*( (log(player_5(3)*exp(-alpha*rb2+qb)+player_5(2)*exp(-alpha*rm2+qb)+player_5(1)*sow2*exp(-alpha*rno2+qn)+player_5(1)*sbw2*player_5(1)*exp(-alpha*rnb2+qn))/alpha))

d_surplus = surplus1-surplus2 % -0.2bps

% least concentrated
[rno1,sno1,rnb1,snb1,rm1,sbm1,rb1,sb1, rhob1, sbw1, sow1,markup11,markup21] = Solve_local(F,sigma,psim,psiw,alpha,k,player_95(3),player_95(1),player_95(2),qn,qb,eta,rhoo);
% shut down cannibalization
[rno2,sno2,rnb2,snb2,rm2,sbm2,rb2,sb2, rhob2, sbw2, sow2,markup12,markup22] = Solve_local_nocan(F,sigma,psim,psiw,alpha,k,player_95(3),player_95(1),player_95(2),qn,qb,eta,rhoo);

d_rate = rhob1-rhob2 %0.5bps
d_local_financed = 100*(sbw1*player_95(1)*player_95(3)-sbw2*player_95(1)*player_95(3))
%-5.9bps
d_mort_rate = 100*(rnb1-rnb2) % 0.05bps
d_bank_rate = 100*(rb1-rb2) % 0.0bps
surplus1 =annuity_factor*( (log(player_95(3)*exp(-alpha*rb1+qb)+player_95(2)*exp(-alpha*rm1+qb)+player_95(1)*sow1*exp(-alpha*rno1+qn)+player_95(1)*sbw1*player_95(1)*exp(-alpha*rnb1+qn))/alpha))
surplus2 =annuity_factor*( (log(player_95(3)*exp(-alpha*rb2+qb)+player_95(2)*exp(-alpha*rm2+qb)+player_95(1)*sow2*exp(-alpha*rno2+qn)+player_95(1)*sbw2*player_95(1)*exp(-alpha*rnb2+qn))/alpha))

d_surplus = 100*(surplus1-surplus2) % -0.1bps

%% mean MSA
% first -- cannibalization 
sigma = 0.3

% across markets 
[rno1,sno1,rnb1,snb1,rm1,sbm1,rb1,sb1, rhob1, sbw1, sow1,markup11,markup21] = Solve_local(F,sigma,psim,psiw,alpha,k,player_50(3),player_50(1),player_50(2),qn,qb,eta,rhoo);
% shut down cannibalization
[rno2,sno2,rnb2,snb2,rm2,sbm2,rb2,sb2, rhob2, sbw2, sow2,markup12,markup22] = Solve_local_nocan(F,sigma,psim,psiw,alpha,k,player_50(3),player_50(1),player_50(2),qn,qb,eta,rhoo);

d_rate = rhob1-rhob2 %0.009%
d_local_financed = 100*(sbw1*player_50(1)*player_50(3)-sbw2*player_50(1)*player_50(3))
%-6.3bps
d_mort_rate = 100*(rnb1-rnb2) % 0.0786bps
d_bank_rate = 100*(rb1-rb2) 
surplus1 =annuity_factor*( (log(player_50(3)*exp(-alpha*rb1+qb)+player_50(2)*exp(-alpha*rm1+qb)+player_50(1)*sow1*exp(-alpha*rno1+qn)+player_50(1)*sbw1*player_50(1)*exp(-alpha*rnb1+qn))/alpha))
surplus2 =annuity_factor*( (log(player_50(3)*exp(-alpha*rb2+qb)+player_50(2)*exp(-alpha*rm2+qb)+player_50(1)*sow2*exp(-alpha*rno2+qn)+player_50(1)*sbw2*player_50(1)*exp(-alpha*rnb2+qn))/alpha))

d_surplus = surplus1-surplus2 

%%
%%%%%%%%%%%%%%%%%%%%%
% second -- funding 
rno = 4.52;
rnb = 4.45;
rb = 4.4;
rm = 4.25
outside = 1-0.2-0.24-0.18-0.05;

sigma = 0.3
F= 15754*354500 % mean number of potential buyers * average mortgage size 
alpha = 1.14;
qb = 2.43; 
qn = 1.67;
k = 3.21;
psim = 3.5;
psiw = 3.28;
eta = 0.09;
rhob = 3.69;
rhoo = 4.38;
%% find mean, top decile and bottom decile 
player_5 = [11;7;6]; % nLS, nNLS, nMB, nFB
player_50 = [24;11;8]; % mean 
player_95 = [44;12;10];

nplayers_5 = sum(player_5); 
global alpha qn qb eta kappa psim psiw r_initial r_initial v0 s_output   %outside option: v0

[~,~,~,~,~,~,~,~, rhob1, sbw1, sow1,~,~] = Solve_local(F,sigma,psim,psiw,alpha,k,player_5(3),player_5(1),player_5(2),qn,qb,eta,rhoo);
v0 = exp(-alpha*0); 
r_initial = ones(nplayers_5,1);
s_output = zeros(nplayers_5,1);

nLS = floor(player_5(1)*sbw1*player_5(3));
nNLS = player_5(1)-nLS;
nMB = player_5(2);
nFB = player_5(3);


findFixedPoints(1, nLS, nNLS, nMB, nFB,rhob1,rhoo);
rb1=r_initial(nLS+nNLS+nMB+1) %4.4126
rnb1 = r_initial(1)
rm1 = r_initial(nLS+nNLS+1)
rno1 = r_initial(nLS+1)
credit1 = sum(s_output)


findFixedPoints(1, nLS, nNLS, nMB+nFB, 0,rhob1,rhoo);
rb2=r_initial(nLS+nNLS+nMB+1) % 4.4062
rnb2 = r_initial(1)
rm2 = r_initial(nLS+nNLS+1)
rno2 = r_initial(nLS+1)
credit2 = sum(s_output)
% outcomes
d_mort_rate = 100*(rnb1-rnb2) %0.0015
d_bank_rate = 100*(rb1-rb2) %0.57

d_credit = 100*(credit1-credit2) % -0.0560


%% least concentrated 

rno = 4.52;
rnb = 4.45;
rb = 4.4;
rm = 4.25
outside = 1-0.2-0.24-0.18-0.05;

sigma = 0.3
F= 15754*354500 % mean number of potential buyers * average mortgage size 
alpha = 1.14;
qb = 2.43; 
qn = 1.67;
k = 3.21;
psim = 3.5;
psiw = 3.28;
eta = 0.09;
rhob = 3.69;
rhoo = 4.38;
%% find mean, top decile and bottom decile 
player_5 = [11;7;6]; % nLS, nNLS, nMB, nFB
player_50 = [24;11;8]; % mean 
player_95 = [44;12;10];

nplayers_95 = sum(player_95); 
global alpha qn qb eta kappa psim psiw r_initial r_initial v0 s_output   %outside option: v0

[~,~,~,~,~,~,~,~, rhob1, sbw1, sow1,~,~] = Solve_local(F,sigma,psim,psiw,alpha,k,player_95(3),player_95(1),player_95(2),qn,qb,eta,rhoo);
v0 = exp(-alpha*0); 
r_initial = ones(nplayers_95,1);
s_output = zeros(nplayers_95,1);
markup_output = zeros(nplayers_95,1);
add_mp_output = zeros(player_95(3),1);
std_mp_output = zeros(nplayers_95,1);

nLS = floor(player_95(1)*sbw1*player_95(3));
nNLS = player_95(1)-nLS;
nMB = player_95(2);
nFB = player_95(3);


findFixedPoints(1, nLS, nNLS, nMB, nFB,rhob1,rhoo);
rb1=r_initial(nLS+nNLS+nMB+1) %4.4126
rnb1 = r_initial(1)
rm1 = r_initial(nLS+nNLS+1)
rno1 = r_initial(nLS+1)
credit1 = sum(s_output)


findFixedPoints(1, nLS, nNLS, nMB+nFB, 0,rhob1,rhoo);
rb2=r_initial(nLS+nNLS+nMB+1) % 4.4062
rnb2 = r_initial(1)
rm2 = r_initial(nLS+nNLS+1)
rno2 = r_initial(nLS+1)
credit2 = sum(s_output)
% outcomes
d_mort_rate = 100*(rnb1-rnb2) % 0.0bps
d_bank_rate = 100*(rb1-rb2) % 0.8322bps

d_credit = 100*(credit1-credit2) % 4.5bps


rno = 4.52;
rnb = 4.45;
rb = 4.4;
rm = 4.25
outside = 1-0.2-0.24-0.18-0.05;

%% mean
sigma = 0.3
F= 15754*354500 % mean number of potential buyers * average mortgage size 
alpha = 1.14;
qb = 2.43; 
qn = 1.67;
k = 3.21;
psim = 3.5;
psiw = 3.28;
eta = 0.09;
rhob = 3.69;
rhoo = 4.38;
%% find mean, top decile and bottom decile 
player_5 = [11;7;6]; % nLS, nNLS, nMB, nFB
player_50 = [24;11;8]; % mean 
player_95 = [44;12;10];

nplayers_50 = sum(player_50); 
global alpha qn qb eta kappa psim psiw r_initial r_initial v0 s_output   %outside option: v0

[~,~,~,~,~,~,~,~, rhob1, sbw1, sow1,~,~] = Solve_local(F,sigma,psim,psiw,alpha,k,player_50(3),player_50(1),player_50(2),qn,qb,eta,rhoo);
v0 = exp(-alpha*0); 
r_initial = ones(nplayers_50,1);
s_output = zeros(nplayers_50,1);
markup_output = zeros(nplayers_50,1);
add_mp_output = zeros(player_50(3),1);
std_mp_output = zeros(nplayers_50,1);

nLS = floor(player_50(1)*sbw1*player_50(3));
nNLS = player_50(1)-nLS;
nMB = player_50(2);
nFB = player_50(3);


findFixedPoints(1, nLS, nNLS, nMB, nFB,rhob1,rhoo);
rb1=r_initial(nLS+nNLS+nMB+1) %4.4126
rnb1 = r_initial(1)
rm1 = r_initial(nLS+nNLS+1)
rno1 = r_initial(nLS+1)
credit1 = sum(s_output)


findFixedPoints(1, nLS, nNLS, nMB+nFB, 0,rhob1,rhoo);
rb2=r_initial(nLS+nNLS+nMB+1) % 4.4062
rnb2 = r_initial(1)
rm2 = r_initial(nLS+nNLS+1)
rno2 = r_initial(nLS+1)
credit2 = sum(s_output)
% outcomes
d_mort_rate = 100*(rnb1-rnb2) % 0.0bps
d_bank_rate = 100*(rb1-rb2) % 0.7070bps

d_credit = 100*(credit1-credit2) % 4.5bps